package com.yt.controller;

import com.yt.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Classname OldController
 * @Description
 * @Date 2021/11/9 14:14
 * @Create by 杨涛
 */
@Controller
public class OldController {

    @RequestMapping("/geta")
    @ResponseBody
    public String getA(){
        return "a";
    }
    @Resource
    UserMapper userMapper;

    @RequestMapping("/fmt/index")
    public String getIndex(Map<String, String> result) {
        result.put("u",userMapper.getUserById(1).toString());
        result.put("a","aaa");
        result.put("resultkey", "freemarker");
        return "test";
    }

    @RequestMapping("/fmt/model")
    public String getIndex(Model model) {
        System.out.println("model");
        model.addAttribute("u",userMapper.getUserById(2) );
        model.addAttribute("a","bbb");
        model.addAttribute("resultkey", "freemarker model");
        return "test";
    }

    @RequestMapping("/fmt/request")
    public String getIndex(HttpServletRequest request) {
        System.out.println("model");
        request.setAttribute( "u",userMapper.getUserById(3) );
        request.setAttribute("a","ccc");
        request.setAttribute("resultkey", "freemarker request");
        return "test";
    }
    @RequestMapping("/fmt/mav/{id}")
    public ModelAndView getIndex( @PathVariable("id") int uid ) {
         ModelAndView mav=new ModelAndView("test");
        System.out.println("model");
        mav.addObject("u",userMapper.getUserById(uid) );
        mav.addObject("a","ddd");
        mav.addObject("resultkey", "freemarker ModelAndView");
        return mav;
    }

    @RequestMapping("/fmt/session/{id}")
    public ModelAndView getIndex(@PathVariable("id") int uid , HttpSession session) {
        ModelAndView mav=new ModelAndView("test");
        System.out.println("session");
        session.setAttribute("u",userMapper.getUserById(uid) );
        session.setAttribute("a","ddd");
        session.setAttribute("resultkey", "freemarker ModelAndView");
        return mav;
    }

    @RequestMapping("/fmt/session2")
    public String getIndex( ) {
        return "test";
    }
}
